package com.mzsoft.ai;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.mzsoft.ai.service.OperateDataService2;
import com.mzsoft.ai.util.ExcelUtil;
import com.mzsoft.ai.util.ReadTemplate2Data;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.mzsoft.ai.utils.file.FileUtils;

/**
 * 导入模板处理
 * @author libq
 */
public class TemplateImportApplication {

    private static Logger logger = LoggerFactory.getLogger(TemplateImportApplication.class);

    public static void main(String[] args)  {

        String userDir = System.getProperty("user.dir");
        String configPath = userDir+"/模板导入配置文件/servers.json";
        logger.info("userDir:"+userDir);
        if(!new File(configPath).exists()){
            throw new RuntimeException("配置服务器文件【"+configPath+"】不存在!");
        }

        String appServerMapStr = FileUtils.ReadFile(configPath);
        logger.debug("appServerMapStr:"+appServerMapStr);
        Map<String,Object> appServerMap = JSON.parseObject(appServerMapStr, Map.class);
        logger.debug("jdbc链接信息:"+JSON.toJSONString(appServerMap));

        for(String businessCode : appServerMap.keySet()){
            operateBusinessExcelData(businessCode,appServerMap);
        }
    }

    /**
     * 业务操作不同文件夹下的excel数据
     * @param businessCode 业务编码
     * @param appServerMap 应用服务器配置
     */
    private static void operateBusinessExcelData(String businessCode, Map<String, Object> appServerMap) {

        logger.debug("operate Template data start ......");
        String basePath = System.getProperty("user.dir");

        Map<String,Object> appServer = (Map<String, Object>) appServerMap.get(businessCode);
        String folderPath = (String) appServer.get("folderPath");

        List<String> fileList = FileUtils.getAllFileName(basePath+folderPath, true, "file");
        logger.debug("fileList:"+JSON.toJSONString(fileList));
        for(String filePath : fileList){

            if(!filePath.contains(ExcelUtil.OFFICE_EXCEL_XLS) && !filePath.contains(ExcelUtil.OFFICE_EXCEL_XLSX)){
                continue;
            }

            //读取excel数据
            Map<String, Object> resultMap = ReadTemplate2Data.readMultiSheetData(filePath);
            logger.debug("excel读取转换后数据："+resultMap);

            //将excel中附件写入临时表
            operateMultiBusinessData(businessCode,appServer,resultMap);
        }

        logger.debug("operate Template excel data end ......");
    }

    private static void operateMultiBusinessData(String businessCode, Map<String, Object> appServer, Map<String, Object> resultMap) {
        logger.debug("operate Template data start ......");

        String folderPath = (String) appServer.get("folderPath");
        List<String> sqlList = (List<String>) appServer.get("sqlList");
        String fileBasePath = System.getProperty("user.dir") + File.separator + "excel" + File.separator;//文件的基础路径
        try {
            //将excel中附件写入临时表
            OperateDataService2.operateMultiData(businessCode,appServer,resultMap);

            //将临时表数据写入到目标表
            OperateDataService2.insertData(folderPath,sqlList);
        } catch (Exception e) {
            e.printStackTrace();
            FileUtils.fileLinesWrite(fileBasePath + "导入异常文件.txt",e.getMessage(),true);
        }

        logger.debug("operate business data start ......");
    }
}
